package com.example.show_auto.ui.utils

import android.os.Build
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/**
 * OPPO设备专用兼容性修复
 * 针对OPPO Reno7 SE等机型的渲染问题进行特殊处理
 */
object OppoCompatibilityFix {
    
    /**
     * 检查是否为OPPO Reno系列
     */
    fun isOppoRenoSeries(): Boolean {
        return Build.MODEL.contains("Reno", ignoreCase = true) ||
               Build.MODEL.contains("PEEM00", ignoreCase = true) ||
               Build.MODEL.contains("PEAM00", ignoreCase = true)
    }
    
    /**
     * 检查是否为问题机型
     */
    fun isProblematicOppoDevice(): Boolean {
        return DeviceCompatibility.isOppoDevice() && (
            isOppoRenoSeries() ||
            Build.MODEL.contains("A", ignoreCase = true) ||
            Build.VERSION.SDK_INT in 30..33 // Android 11-13
        )
    }
    
    /**
     * 获取修复后的卡片高度
     */
    @Composable
    fun getFixedCardHeight(originalHeight: Dp): Dp {
        return if (isProblematicOppoDevice()) {
            originalHeight * 0.85f // 减少15%
        } else {
            originalHeight
        }
    }
    
    /**
     * 获取修复后的间距
     */
    @Composable
    fun getFixedSpacing(originalSpacing: Dp): Dp {
        return if (isProblematicOppoDevice()) {
            originalSpacing * 0.75f // 减少25%
        } else {
            originalSpacing
        }
    }
    
    /**
     * 获取修复后的字体大小
     */
    @Composable
    fun getFixedTextSize(originalSize: androidx.compose.ui.unit.TextUnit): androidx.compose.ui.unit.TextUnit {
        return if (isProblematicOppoDevice()) {
            originalSize * 0.9f // 减少10%
        } else {
            originalSize
        }
    }
    
    /**
     * 强制重绘修饰符
     */
    @Composable
    fun Modifier.forceRecomposition(): Modifier {
        return if (isProblematicOppoDevice()) {
            var trigger by remember { mutableStateOf(0) }
            
            LaunchedEffect(Unit) {
                kotlinx.coroutines.delay(50)
                trigger++
            }
            
            this
        } else {
            this
        }
    }
    
    /**
     * 修复后的滚动修饰符
     */
    @Composable
    fun Modifier.fixedVerticalScroll(): Modifier {
        return if (isProblematicOppoDevice()) {
            this.verticalScroll(rememberScrollState())
                .fillMaxSize()
        } else {
            this.verticalScroll(rememberScrollState())
        }
    }
    
    /**
     * 获取修复后的卡片颜色
     */
    @Composable
    fun getFixedCardColors(isSelected: Boolean = false): CardColors {
        return if (isProblematicOppoDevice()) {
            // 使用更明确的颜色对比
            if (isSelected) {
                CardDefaults.cardColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    contentColor = MaterialTheme.colorScheme.onPrimaryContainer
                )
            } else {
                CardDefaults.cardColors(
                    containerColor = MaterialTheme.colorScheme.surface,
                    contentColor = MaterialTheme.colorScheme.onSurface
                )
            }
        } else {
            if (isSelected) {
                CardDefaults.cardColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.3f)
                )
            } else {
                CardDefaults.cardColors()
            }
        }
    }
    
    /**
     * 获取修复后的文本字段颜色
     */
    @Composable
    fun getFixedTextFieldColors(): TextFieldColors {
        return if (isProblematicOppoDevice()) {
            OutlinedTextFieldDefaults.colors(
                focusedBorderColor = MaterialTheme.colorScheme.primary,
                unfocusedBorderColor = MaterialTheme.colorScheme.outline,
                focusedLabelColor = MaterialTheme.colorScheme.primary,
                unfocusedLabelColor = MaterialTheme.colorScheme.onSurfaceVariant,
                cursorColor = MaterialTheme.colorScheme.primary,
                focusedTextColor = MaterialTheme.colorScheme.onSurface,
                unfocusedTextColor = MaterialTheme.colorScheme.onSurface
            )
        } else {
            OutlinedTextFieldDefaults.colors()
        }
    }
    
    /**
     * 修复后的布局容器
     */
    @Composable
    fun FixedLayoutContainer(
        modifier: Modifier = Modifier,
        content: @Composable ColumnScope.() -> Unit
    ) {
        if (isProblematicOppoDevice()) {
            // 对于问题设备，添加额外的布局稳定性
            Column(
                modifier = modifier
                    .fillMaxSize()
                    .forceRecomposition(),
                content = content
            )
        } else {
            Column(
                modifier = modifier.fillMaxSize(),
                content = content
            )
        }
    }
    

    
    /**
     * 应用全局修复
     */
    @Composable
    fun applyGlobalFixes() {
        if (isProblematicOppoDevice()) {
            // 强制重绘整个界面
            var recompositionTrigger by remember { mutableStateOf(0) }
            
            LaunchedEffect(Unit) {
                kotlinx.coroutines.delay(100)
                recompositionTrigger++
            }
        }
    }
}
